package com.cuixichang.minimalism.acl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cuixichang.minimalism.basal.base.Validate;
import com.cuixichang.minimalism.basal.enums.IsEnableEnum;
import com.cuixichang.minimalism.basal.enums.SubassemblyTypeEnum;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.function.BiFunction;

@Component
public class SelectSubassemblyDesign extends SubassemblyDesignAbs{
    @Override
    public SubassemblyTypeEnum getType() {
        return SubassemblyTypeEnum.Select;
    }

    public String example() {
        return "{\"Code\":\"Value\",···}";
    }
    @Override
    public Validate structureValidate(String structure) {
        boolean isJsonObj = JSONUtil.isJsonObj(structure);
        return Validate.builder().checked(isJsonObj).message(isJsonObj ? IsEnableEnum.YES.getDesc() : "下拉框阈值预设检测不通过").build();
    }

    @Override
    public Validate valueValidate(String structure, String value) {
        String key = Objects.isNull(value)? StrUtil.EMPTY:value;
        JSONObject jsonObject = JSONUtil.parseObj(structure);
        boolean pass = jsonObject.containsKey(key);
        return Validate.builder().checked(pass).message(pass ? IsEnableEnum.YES.getDesc():"下拉框选项参数不匹配").build();
    }

    @Override
    public String previewValue(String structure, Object value) {
        String key = Objects.isNull(value)? StrUtil.EMPTY:String.valueOf(value);
        Validate validate = valueValidate(structure, key);
        if(Objects.nonNull(validate) && validate.getChecked()){
            JSONObject jsonObject = JSONUtil.parseObj(structure);
            Object val = jsonObject.get(key);
            return Objects.nonNull(val)?String.valueOf(val):StrUtil.EMPTY;
        }
        return StrUtil.EMPTY;
    }

    @Override
    public Object resultValue(String value) {
        return value;
    }

    @Override
    public String resultValue(Object value) {
        return String.valueOf(value);
    }
}
